home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v9n07.arc / COMINPUT.FNC < prev    next >
Text File  |  1990-03-16  |  3KB  |  77 lines

  1.  
  2. 'The ComInput Function
  3.  
  4. DEFINT A-Z
  5.  
  6. DECLARE FUNCTION ComInput% (Handle%, TimeOut, Buffer$, Term$, ErrCode%)
  7.  
  8. OPEN "COM1:1200, N, 8, 1, BIN" FOR RANDOM AS #1         'Open the COM port
  9.  
  10. Handle = 1                              'Specify the file handle
  11. TimeOut = 5                             'Set time-out to 5 seconds
  12. Buffer$ = SPACE$(256)                   'Make a 256 character buffer
  13. Term$ = CHR$(13) + CHR$(10)             'Specify the line terminator string
  14.  
  15. DO                                      'Read a line of data
  16.    Pointer = ComInput(Handle, TimeOut, Buffer$, Term$, ErrCode)
  17.    PRINT LEFT$(Buffer$, Pointer);       'Print the part of buffer filled
  18.    IF ErrCode <> 69 THEN PRINT          'If the buffer didn't overflow
  19.                                         '  print a CR,LF
  20. LOOP UNTIL ErrCode = 24                 'Get another line unless we timed out
  21.  
  22. CLOSE #1                                'Close the port
  23.  
  24. '***************************************************************************
  25. 'Function ComInput
  26. '    Reads characters from a COM port until a terminating string is read
  27. '    or the length of the buffer is exceded or a time-out occurs.  Replaces
  28. '    BASIC's "LINE INPUT #" routine.
  29. 'Inputs:
  30. '    Handle   - The file handle used to refer to the COM port
  31. '    TimeOut  - Number of seconds to wait for each character
  32. '    Buffer$  - Padded string which will hold characters read from the port
  33. '    Term$    - String used to detect the end of a line
  34. 'Returns:
  35. '    FUNCTION - returns a pointer into the buffer where the line ends
  36. '    ErrCode  - Flags the success or failure of the function
  37. '               0 = No error, 24 = "Device Timeout", 69 = "Buffer Overflow"
  38. '***************************************************************************
  39. '
  40. FUNCTION ComInput (Handle, TimeOut, Buffer$, Term$, ErrCode) STATIC
  41.    
  42.     ErrCode = 0                         'No errors yet
  43.     TermLen = LEN(Term$)                'Save length of terminator string
  44.     BufLen = LEN(Buffer$)               'Save length of buffer
  45.     T& = TIMER + TimeOut                'Set the value for time-out
  46.  
  47.     FOR Ptr = 1 TO BufLen               'Assume we will fill the whole buffer
  48.        DO UNTIL LOC(Handle)             'Wait for a character to come in port
  49.           IF TIMER > T& THEN            'Have we waited too long?
  50.              ErrCode = 24               'Set BASIC's "Device timeout" error
  51.              EXIT FOR                   'Bail out of loops
  52.           END IF
  53.        LOOP
  54.                                         'Read the character into the buffer
  55.        MID$(Buffer$, Ptr) = INPUT$(1, Handle)
  56.                                         'If looking for a terminating string,
  57.        IF TermLen > 0 AND Ptr >= TermLen THEN
  58.           IF MID$(Buffer$, Ptr - TermLen + 1, TermLen) = Term$ THEN EXIT FOR
  59.        END IF
  60.  
  61.     NEXT
  62.  
  63.     IF Ptr > BufLen THEN                'Did we go off the end of the buffer?
  64.        Ptr = Ptr - 1                    'Fix the pointer
  65.        ErrCode = 69                     'Set BASIC's "Com Buffer Overflow"
  66.     ELSE                                '  error
  67.        IF Ptr > TermLen THEN
  68.           Ptr = Ptr - TermLen           'Set pointer to before terminator
  69.        END IF
  70.     END IF
  71.  
  72.     ComInput = Ptr                      'Assign the function
  73.  
  74. END FUNCTION
  75.  
  76.  
  77.